<?php
	class Khachsan_model extends CI_Model 
	{
    	function LayTatCaKhachSan_NhaCungCap() 
		{
    	    $post_per_page = 20;
        	$num = 0;
	        if ($this->uri->segment(3))
    	        $num = $this->uri->segment(3);

        	$strSQL = "SELECT KhachSan.*, NhaCungCap.TinhTrangTaiKhoan, NhaCungCap.Ten AS TenNCC
						FROM KhachSan
						JOIN NhaCungCap ON KhachSan.NhaCungCap = NhaCungCap.MaNCC
						WHERE NhaCungCap.TinhTrangTaiKhoan IS NOT NULL
						ORDER BY KhachSan.Ten ASC
						LIMIT $num, $post_per_page";
	        $query = $this->db->query($strSQL);

    	    $total_query = "SELECT KhachSan.*, NhaCungCap.TinhTrangTaiKhoan, NhaCungCap.Ten AS TenNCC
							FROM KhachSan
							JOIN NhaCungCap ON KhachSan.NhaCungCap = NhaCungCap.MaNCC
							WHERE NhaCungCap.TinhTrangTaiKhoan IS NOT NULL";

	        $this->load->library('pagination');
    	    $config['base_url'] = base_url() . 'index.php/khachsan/admin_index';
	        $config['total_rows'] = $this->db->query($total_query)->num_rows();
    	    $config['per_page'] = $post_per_page;
        	$this->pagination->initialize($config);

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;
	    }

    	function Tim($key) 
		{
    	    $post_per_page = 20;
        	$num = 0;
	        if ($this->uri->segment(3))
    	        $num = $this->uri->segment(3);

        	$strSQL = "SELECT ks.*, ncc.TinhTrangTaiKhoan, ncc.Ten AS TenNCC
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
						AND (ks.MaKS = '$key' OR ks.Ten LIKE '%$key%' OR ks.Tinh_ThanhPho LIKE '%$key%')
						ORDER BY ks.Ten ASC
						LIMIT $num, $post_per_page";
	        $query = $this->db->query($strSQL);

    	    $total_query = "SELECT ks.*, ncc.TinhTrangTaiKhoan, ncc.Ten AS TenNCC
							FROM KhachSan ks
							JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
							WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
							AND (ks.MaKS = '$key' OR ks.Ten LIKE '%$key%' OR ks.Tinh_ThanhPho LIKE '%$key%')";

	        $this->load->library('pagination');
    	    $config['base_url'] = base_url() . 'index.php/khachsan/admin_index';
	        $config['total_rows'] = $this->db->query($total_query)->num_rows();
    	    $config['per_page'] = $post_per_page;
	        $this->pagination->initialize($config);

    	    if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
        	        $data[] = $row;
            	}
	            return $data;
    	    }
        	return NULL;
	    }

	    function LayKhachSan_NhaCungCap($maks) 
		{
	        $this->db->select('KhachSan.*, NhaCungCap.Ten AS TenNCC, NhaCungCap.TinhTrangTaiKhoan');
    	    $this->db->from('KhachSan');
        	$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
	        $this->db->where('KhachSan.MaKS', $maks);
    	    $query = $this->db->get();
        	if ($query->num_rows() > 0) 
			{
    	        foreach ($query->result() as $row) 
				{
            	    $data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function LayTatCaKhachSan_DangHopTac() 
		{
	        $this->db->select('KhachSan.*');
    	    $this->db->from('KhachSan');
        	$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
	        $this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
    	    $query = $this->db->get();

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;
	    }
		
		function LayMaKS_ajax($key)
		{
			$strSQL = "SELECT ks.MaKS AS Ten
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
						AND ks.MaKS LIKE '%$key%'
						ORDER BY ks.MaKS ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;			
		}		
		
		function LayTenKhachSan_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ks.Ten
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
						AND ks.Ten LIKE '%$key%'
						ORDER BY ks.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;			
		}
		
		function LayTenKhachSan_Category_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ks.Ten AS label, 'Khách sạn' AS category
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan = 1
						AND ks.Ten LIKE '%$key%'
						ORDER BY ks.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;			
		}		
		
		function LayThanhPho_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ks.Tinh_ThanhPho AS Ten
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
						AND ks.Tinh_ThanhPho LIKE '%$key%'
						ORDER BY ks.Tinh_ThanhPho ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;			
		}
		
		function LayThanhPho_Category_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ks.Tinh_ThanhPho AS label, 'Thành phố' AS category
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan = 1
						AND ks.Tinh_ThanhPho LIKE '%$key%'
						ORDER BY ks.Tinh_ThanhPho ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;			
		}				
		
	    function LayTatCaQuocGia() 
		{
	        $this->db->select('KhachSan.QuocGia');
    	    $this->db->from('KhachSan');
        	$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
	        $this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
    	    $this->db->distinct();
	        $query = $this->db->get();

    	    if ($query->num_rows() > 0) 
			{
    	        foreach ($query->result() as $row) 
				{
            	    $data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function LayThanhPho($quocgia) 
		{
	        $this->db->select('KhachSan.Tinh_ThanhPho');
    	    $this->db->from('KhachSan');
        	$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
	        $this->db->where('KhachSan.QuocGia', $quocgia);
    	    $this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
	        $this->db->distinct();
    	    $query = $this->db->get();

        	if ($query->num_rows() > 0) 
			{
    	        foreach ($query->result() as $row) 
				{
            	    $data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function LayKhachSan($quocgia, $thanhpho) 
		{
	        $this->db->select('KhachSan.Ten, KhachSan.MaKS');
    	    $this->db->from('KhachSan');
        	$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
	        $this->db->where('KhachSan.QuocGia', $quocgia);
    	    $this->db->where('KhachSan.Tinh_ThanhPho', $thanhpho);
        	$this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
	        $query = $this->db->get();

    	    if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
        	        $data[] = $row;
            	}
	            return $data;
    	    }
        	return NULL;
	    }

	    function LayTenQuocGia($maks) 
		{
	        $query = $this->db->get_where('KhachSan', array('MaKS' => $maks));

    	    if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
        	        $ten = $row->QuocGia;
            	}
	            return $ten;
    	    }
        	return NULL;
	    }

	    function LayTenThanhPho($maks) 
		{
        	$query = $this->db->get_where('KhachSan', array('MaKS' => $maks));

	        if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
        	        $ten = $row->Tinh_ThanhPho;
            	}
	            return $ten;
    	    }
        	return NULL;
	    }

	    function CapNhat($data, $maks) 
		{
	        $this->db->update('KhachSan', $data, array('MaKS' => $maks));
    	    return $this->db->affected_rows();
	    }

    	function layQuocGia() 
		{
    	    $this->db->distinct();
        	$this->db->select('KhachSan.QuocGia');
	        $this->db->from('KhachSan');
			$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
			$this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
    	    $query = $this->db->get();
        	if ($query->num_rows() > 0) 
			{
    	        foreach ($query->result() as $row) 
				{
            	    $data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function TimThanhPho($country) 
		{
	        $this->db->distinct();
    	    $this->db->select('KhachSan.Tinh_ThanhPho');
        	$this->db->from('KhachSan');
			$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
			$this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
	        $this->db->where('KhachSan.QuocGia', $country);
    	    $query = $this->db->get();
        	if ($query->num_rows() > 0) 
			{
    	        foreach ($query->result() as $row) 
				{
            	    $data[] = $row;
	            }
    	        return $data;
        	} 
			else 
			{
        	    return NULL;
	        }
    	}

	    function TimQuan($city) 
		{
	        $this->db->distinct();
    	    $this->db->select('KhachSan.Quan');
        	$this->db->from('KhachSan');
			$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
			$this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
	        $this->db->where('KhachSan.Tinh_ThanhPho', $city);
    	    $query = $this->db->get();
	        if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
        	        $data[] = $row;
            	}
	            return $data;
    	    } 
			else 
			{
    	        return NULL;
        	}
	    }

	    function TimKhachSanNhanh($quick_search, $dateCheckin) 
		{
    	    $strSQL = "SELECT ks.MaKS as MaKhachSan, ks.Ten as TenKhachSan, ks.SoNha as SoNha,ks.Duong as Duong, ks.Quan as Quan,ks.Tinh_ThanhPho as Tinh_ThanhPho,ks.QuocGia as QuocGia,ks.ThongTin as ThongTin,ks.Diem as Diem, lp.GiaTruocKhuyenMai as GiaTruocKhuyenMai, ha.HinhNho as Hinh,ks.TieuChuan as TieuChuan
						FROM KhachSan ks
						LEFT JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						LEFT JOIN LoaiPhong lp ON ks.MaKS = lp.KhachSan
						LEFT JOIN HinhAnh ha ON ks.MaKS = ha.KhachSan
						WHERE (ks.Tinh_ThanhPho LIKE '%$quick_search%' OR ks.Ten LIKE '%$quick_search%')
							  AND ncc.TinhTrangTaiKhoan = 1 
							  AND GiaTruocKhuyenMai = (SELECT min(GiaTruocKhuyenMai)
													   FROM KhachSan ks1
													   JOIN LoaiPhong lp1 ON ks1.MaKS = lp1.KhachSan
														WHERE ks.MaKS = ks1.MaKS)
							  AND EXISTS (	SELECT * 
							  				FROM KhachSan ks2
											JOIN LoaiPhong lp2 ON ks2.MaKS = lp2.KhachSan
											JOIN PhongTrongTheoNgay pt ON lp2.MaLP = pt.LoaiPhong
  											WHERE ks2.MaKS = ks.MaKS
											AND pt.Ngay = '$dateCheckin'
											AND pt.SoLuongPhongTrong > 0)
						GROUP BY ks.MaKS
						ORDER BY ks.Diem DESC";
        	$query = $this->db->query($strSQL);
	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
    	            $data[] = $row;
        	    }
            	return $data;
	        } 
			else 
			{
	            return NULL;
    	    }
	    }

    	function TimKhachSanTuyChon($tieuchi) 
		{
    	    if ($tieuchi[2] != "") 
			{
            	$strSQL = "SELECT ks.MaKS as MaKhachSan, ks.Ten as TenKhachSan, ks.SoNha as SoNha,ks.Duong as Duong, ks.Quan as Quan,ks.Tinh_ThanhPho as Tinh_ThanhPho,ks.QuocGia as QuocGia,ks.ThongTin as ThongTin,ks.Diem as Diem, lp.GiaTruocKhuyenMai as GiaTruocKhuyenMai, ha.HinhNho as Hinh,ks.TieuChuan as TieuChuan
							FROM KhachSan ks
							LEFT JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
							LEFT JOIN LoaiPhong lp ON ks.MaKS = lp.KhachSan
							LEFT JOIN HinhAnh ha ON ks.MaKS = ha.KhachSan
							WHERE ncc.TinhTrangTaiKhoan = 1 
								AND ks.QuocGia LIKE '$tieuchi[0]' 
								AND ks.Tinh_ThanhPho LIKE '$tieuchi[1]' 
								AND ks.Quan LIKE '$tieuchi[2]' 
								AND GiaTruocKhuyenMai = (SELECT min(GiaTruocKhuyenMai)
														FROM KhachSan ks1
														JOIN LoaiPhong lp1 ON ks1.MaKS = lp1.KhachSan
														WHERE ks.MaKS = ks1.MaKS)
							  AND EXISTS (	SELECT * 
							  				FROM KhachSan ks2
											JOIN LoaiPhong lp2 ON ks2.MaKS = lp2.KhachSan
											JOIN PhongTrongTheoNgay pt ON lp2.MaLP = pt.LoaiPhong
  											WHERE ks2.MaKS = ks.MaKS
											AND pt.Ngay = '$tieuchi[3]'
											AND pt.SoLuongPhongTrong > 0)														
							GROUP BY ks.MaKS
							ORDER BY ks.Diem DESC";
	        } 
			else 
			{
            	$strSQL = "SELECT ks.MaKS as MaKhachSan, ks.Ten as TenKhachSan, ks.SoNha as SoNha,ks.Duong as Duong, ks.Quan as Quan,ks.Tinh_ThanhPho as Tinh_ThanhPho,ks.QuocGia as QuocGia,ks.ThongTin as ThongTin,ks.Diem as Diem, lp.GiaTruocKhuyenMai as GiaTruocKhuyenMai, ha.HinhNho as Hinh,ks.TieuChuan as TieuChuan
							FROM KhachSan ks
							LEFT JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
							LEFT JOIN LoaiPhong lp ON ks.MaKS = lp.KhachSan
							LEFT JOIN HinhAnh ha ON ks.MaKS = ha.KhachSan
							WHERE ncc.TinhTrangTaiKhoan = 1 
								AND ks.QuocGia LIKE '$tieuchi[0]' 
								AND ks.Tinh_ThanhPho LIKE '$tieuchi[1]'
								AND GiaTruocKhuyenMai = (SELECT min(GiaTruocKhuyenMai)
														FROM KhachSan ks1
														JOIN LoaiPhong lp1 ON ks1.MaKS = lp1.KhachSan
														WHERE ks.MaKS = ks1.MaKS)
							  AND EXISTS (	SELECT * 
							  				FROM KhachSan ks2
											JOIN LoaiPhong lp2 ON ks2.MaKS = lp2.KhachSan
											JOIN PhongTrongTheoNgay pt ON lp2.MaLP = pt.LoaiPhong
  											WHERE ks2.MaKS = ks.MaKS
											AND pt.Ngay = '$tieuchi[3]'
											AND pt.SoLuongPhongTrong > 0)														
							GROUP BY ks.MaKS
							ORDER BY ks.Diem DESC";
	        }
    	    $query = $this->db->query($strSQL);
        	if ($query->num_rows() > 0) 
			{
    	        foreach ($query->result() as $row) 
				{
            	    $data[] = $row;
	            }
    	        return $data;
	        } 
			else 
			{
            	return NULL;
	        }
    	}

	    function get_records_khachsan() 
		{
	        $khachsan = $this->input->post('makhachsan');
    	    $this->db->select('MaKS, Ten, SoNha, Duong, Quan, Tinh_ThanhPho, QuocGia, ThongTin, TieuChuan, PhiDichVu, VAT');
        	$this->db->from('KhachSan');
	        $this->db->where('MaKS', $khachsan);
    	    $q = $this->db->get();
        	if ($q->num_rows() > 0) 
			{
    	        foreach ($q->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    $data[] = $khachsan;
            	return $data;
	        }
    	}

    	function get_records_khachsan_($makhachsan) 
		{
    	    $this->db->select('MaKS, Ten, SoNha, Duong, Quan, Tinh_ThanhPho, QuocGia, ThongTin, TieuChuan, PhiDichVu, VAT');
        	$this->db->from('KhachSan');
    	    $this->db->where('MaKS', $makhachsan);
        	$q = $this->db->get();
	        if ($q->num_rows() > 0) 
			{
        	    foreach ($q->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	}
		
		function laymaKS($mancc){
			$query = $this->db->get_where('KhachSan', array('NhaCungCap' => $mancc));
	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $maks = $row->MaKS;
    	        }
        	    return $maks;
	        }else{
	        	return NULL;
	        }
		}
		
		function LayThongTinKhachSan($maks)
		{
			$strSQL = "SELECT *
						FROM KhachSan ks
						WHERE MaKS = '$maks'";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;			
		}
		
		function khachsantotnhat() {
			$this->db->select('MaKS, KhachSan.Ten as TenKhachSan, Diem, Tinh_ThanhPho, KhachSan.QuocGia');
			$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
			$this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
			$this->db->order_by("Diem", "desc");
			$this->db->limit(13);
			$q = $this->db->get('KhachSan');
			if ($q->num_rows() > 0) {
				foreach ($q->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
		}

		function khachsantieubieu() {
			$this->db->select('KhachSan.MaKS, KhachSan.Ten as TenKhachSan, KhachSan.ThongTin, HinhAnh.HinhLon, HinhAnh.HinhNho, MIN(HinhAnh.MaHA) AS MaHA');
			$this->db->from('KhachSan');
			$this->db->join('NhaCungCap', 'KhachSan.NhaCungCap = NhaCungCap.MaNCC');
			$this->db->join('HinhAnh', 'KhachSan.MaKS = HinhAnh.KhachSan');
			$this->db->where('NhaCungCap.TinhTrangTaiKhoan', 1);
			$this->db->group_by("KhachSan.MaKS");
			$this->db->order_by("KhachSan.TieuChuan desc, KhachSan.Ten asc");
			$this->db->limit(20);
			$q = $this->db->get();
			if ($q->num_rows() > 0) {
				foreach ($q->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
		}

    	function TimKhachSanTrongVung($thanhpho) 
		{
        	$strSQL = 'SELECT ks.MaKS as MaKhachSan, ks.Ten as TenKhachSan, ks.SoNha as SoNha,ks.Duong as Duong, ks.Quan as Quan,ks.Tinh_ThanhPho as Tinh_ThanhPho,ks.QuocGia as QuocGia,ks.ThongTin as ThongTin,ks.Diem as Diem, lp.GiaTruocKhuyenMai as GiaTruocKhuyenMai, ha.HinhNho as Hinh,ks.TieuChuan as TieuChuan
							FROM KhachSan ks
							JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
							JOIN LoaiPhong lp ON ks.MaKS = lp.KhachSan
							LEFT JOIN HinhAnh ha ON ks.MaKS = ha.KhachSan
							WHERE ncc.TinhTrangTaiKhoan = 1 
								AND ks.Tinh_ThanhPho LIKE \'' . $thanhpho . '\' 
								AND GiaTruocKhuyenMai = (SELECT min(GiaTruocKhuyenMai)
														FROM KhachSan ks1
														JOIN LoaiPhong lp1 ON ks1.MaKS = lp1.KhachSan
														WHERE ks.MaKS = ks1.MaKS)
							GROUP BY ks.MaKS
							ORDER BY ks.Diem DESC';
	        $query = $this->db->query($strSQL);
    	    if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
        	        $data[] = $row;
	            }
	            return $data;
    	    } 
			else 
			{
    	        return NULL;
        	}
	    }

		function khachsandangcokhuyenmai() {
			$today = date("Y-m-d");
			$sql = 'select distinct ks.MaKS as MaKhachSan,ks.Ten as TenKhachSan,GiaTriKhuyenMai 
					from ThongTinKhuyenMai ttkm
					JOIN KhachSan ks ON KhachSan = ks.MaKS
					LEFT JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC 
					where ncc.TinhTrangTaiKhoan = 1 AND khachsan = MaKS and NgayKetThuc >= ?';
			$q = $this->db->query($sql, array($today));
			if ($q->num_rows() > 0) {
				foreach ($q->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
		}
	
		function Them($data)
		{
			$this->db->insert('KhachSan', $data);
			return $this->db->affected_rows();
		}

		function LayTenQuocGia_ajax($key)
		{
        	$this->db->select('KhachSan.QuocGia');
	        $this->db->from('KhachSan');
        	$this->db->like('KhachSan.QuocGia', $key);
			$this->db->order_by('KhachSan.QuocGia', "asc");	
			$this->db->distinct();		
	        $query = $this->db->get();
    	    if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;			
		}

		function LayTenThanhPho_ajax($key)
		{
        	$this->db->select('KhachSan.Tinh_ThanhPho');
	        $this->db->from('KhachSan');
        	$this->db->like('KhachSan.Tinh_ThanhPho', $key);
			$this->db->order_by('KhachSan.Tinh_ThanhPho', "asc");	
			$this->db->distinct();		
	        $query = $this->db->get();
    	    if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;			
		}
		
		function LayTenQuan_ajax($key)
		{
        	$this->db->select('KhachSan.Quan');
	        $this->db->from('KhachSan');
        	$this->db->like('KhachSan.Quan', $key);
			$this->db->order_by('KhachSan.Quan', "asc");	
			$this->db->distinct();		
	        $query = $this->db->get();
    	    if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;			
		}
		
		function LayKhachSan_SoSanh($danhsach_maks)
		{
			$strSQL = "SELECT ks.MaKS as MaKhachSan, ks.Ten as TenKhachSan, ks.SoNha as SoNha,ks.Duong as Duong, ks.Quan as Quan,ks.Tinh_ThanhPho as Tinh_ThanhPho,ks.QuocGia as QuocGia,ks.ThongTin as ThongTin,ks.Diem as Diem, lp.GiaTruocKhuyenMai as GiaTruocKhuyenMai, ha.HinhNho as Hinh,ks.TieuChuan as TieuChuan
						FROM KhachSan ks
						LEFT JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						LEFT JOIN LoaiPhong lp ON ks.MaKS = lp.KhachSan
						LEFT JOIN HinhAnh ha ON ks.MaKS = ha.KhachSan
						WHERE ks.MaKS IN ($danhsach_maks"."0)
							  AND ncc.TinhTrangTaiKhoan = 1 
							  AND GiaTruocKhuyenMai = (SELECT min(GiaTruocKhuyenMai)
													   FROM KhachSan ks1
													   JOIN LoaiPhong lp1 ON ks1.MaKS = lp1.KhachSan
														WHERE ks.MaKS = ks1.MaKS)
						GROUP BY ks.MaKS
						ORDER BY ks.Diem DESC";
						
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;			
		}
	}
?>